/********************************************************************************************
 Class Name: NMRMasterLeadEnrichment
 Author: Accenture
 Date: 03/06/2012
 Requirement/Project Name: NMR
 Requirement Description: Class for Enrichment of Master Leads from SLRs
 Version History:0.2 Updating task details as per defect 894.
 ***********************************************************************************************/
 public class NMRMasterLeadEnrichment{
 
     //retrieve the record type Ids for Lead, Task and Account
     Public static Id nmrLeadRecordTypeID = RecordTypeHelper.GetRecordTypeID('Lead', 'NMR Lead');//Lead Recordtype
     public static Id nmrLeadCustIdRecordTypeID = RecordTypeHelper.GetRecordTypeID('Lead', 'NMR Lead with CustomerId');//Lead Redordtype
     Public static Id nmrTaskRecordTypeID = RecordTypeHelper.GetRecordTypeID('Task', 'NS Tasks');//Task RecordType
     Public static Id nmrAccountRecordTypeID = RecordTypeHelper.GetRecordTypeID('Account', 'CSS Customer');//Account Recordtype
     
     /**
     * Method which searches for existing leads/accounts with same phone.
     * If found then the SLR is linked to the corresponding Lead/Account.
     * Else new lead is created.
     */
     public static void findLeadtoEnrich(Map<string,Standardized_Lead_Request__c> slrRecordMap,Set<string> phoneSet)
     {   
         Map<string,Lead> slrLeadMap  = new Map<string,Lead>();//leads with same phone with SLR
         Map<string,Lead> slrLeadMapUpdate  = new Map<string,Lead>();//To add leads to update
         Map<string,Account> slrAccountMapUpdate  = new Map<string,Account>();//Accounts with same phone with SLR
         Map<Id,Cate__c> headingMap  = new Map<Id,Cate__c>();//Headings from SLR
         Map<string,List<Cate__c>> headingMapLead  = new Map<string,List<Cate__c>>();//Headings from Lead
         Map<string, Cate__c> headingMapSLR  = new Map<string, Cate__c>();//Primary Headings from SLR
         List<Lead> leadList = new List<Lead>();
         Set<Id> slrIdSet = new Set<Id>();
         Set<Id> leadIdSet = new Set<Id>(); // Lead id's with same phone with SLR
         list<task> taskList = new list<task>();
         
         //Adding SLR Ids to set
         for(Standardized_Lead_Request__c singleSLR:slrRecordMap.Values()){
             slrIdSet.add(singleSLR.Id);
         }
         //Querying the Leads with same phone as SLR
         for(Lead singleLead:[Select Id, Last_Enrichment_Date__c, Lead_Type__c, Phone, FirstName, LastName, Email, LMCListing_Id__c, Company,Website,
                              Street, City, State, PostalCode, Business_Description__c, NumberOfEmployees, Advertiser_Comments__c, Campaign_Medium__c,
                              Campaign_Source__c, Campaign_Content__c, Campaign_Term__c, Campaign_Category__c, Campaign_Name__c, Sales_Channel__c,
                              Chain_ID__c, NMR_Additional_Phone_1__c, Do_Not_Call_1__c, NMR_Additional_Phone_2__c, Do_Not_Call_2__c, In_Business_Since__c,
                              NMR_Additional_Phone_3__c, Do_Not_Call_3__c, NMR_Additional_Phone_4__c, Do_Not_Call_4__c, NMR_Additional_Phone_5__c,
                              Do_Not_Call_5__c, Offer_Product__c, Flow_Progress__c, AKAs__c, ASCP_Suscriber_Created_Date__c, Offer_Type__c, Ad_Size__c,
                              Ad_Spend__c, Service_Products__c, YP_Id__c, Friendly_Name__c, ECommerce_CID__c, Send_Special_Offers__c, Sales_Lead_Id__c
                              from Lead where Phone IN: phoneSet AND (RecordtypeId =: nmrLeadRecordTypeID OR RecordtypeId =: nmrLeadCustIdRecordTypeID) and IsConverted = false])
                              {
                                     slrLeadMap.put(singleLead.phone,singleLead);
                                     leadIdSet.add(singleLead.Id);
                              }
         //Accounts with same phones as SLR         
         for(Account singleAcc:[Select phone,Id,CSS_ListingTel__c,ownerid,OOR_Secondary_Account_Owner__c from Account where CSS_ListingTel__c IN: phoneSet AND RecordtypeId =: nmrAccountRecordTypeID AND CSS_ListingTel__c != NULL])
         {
             slrAccountMapUpdate.put(singleAcc.CSS_ListingTel__c,singleAcc);
         }
         
         //For Headings in SLR
         for(Cate__c singleCate:[Select Id,Standardized_Lead_Request__r.Source_Lead_Id__c,Standardized_Lead_Request__r.Related_Lead_Id__c,Standardized_Lead_Request__r.Business_Phone__c,Lead__r.phone,Description__c,Heading_Code__c,Restricted_Flag__c,Source__c, Weight__c,Source_Lead_I__c,YP_Cat__c from Cate__c where Standardized_Lead_Request__c IN: slrIdset order by CreatedDate]){  
                   
                 headingMap.put(singleCate.id,singleCate);
                 
                 if(slrRecordMap.containsKey(NMRUtil.formatPhone(singleCate.Standardized_Lead_Request__r.Business_Phone__c))
                     && NMRUtil.isEmpty(slrRecordMap.get(NMRUtil.formatPhone(singleCate.Standardized_Lead_Request__r.Business_Phone__c)).YP_Cat_Id__c))
                     slrRecordMap.get(NMRUtil.formatPhone(singleCate.Standardized_Lead_Request__r.Business_Phone__c)).YP_Cat_Id__c = singleCate.YP_Cat__c;
                 
                 if(!headingMapSLR.containsKey(singleCate.Standardized_Lead_Request__r.Business_Phone__c))
                 {//Use first heading as query auto-sorts based on Headings Id.
                     headingMapSLR.put(NMRUtil.formatPhone(singleCate.Standardized_Lead_Request__r.Business_Phone__c), singleCate);
                 }
             
         }
         
         //SLR to Lead or Account Map Logic
         for(string phoneStr:slrRecordMap.keyset()){
             //If Account is present to match the SLR
             if(slrAccountMapUpdate.containsKey(phoneStr)){                 
                 slrRecordMap.get(phoneStr).Account_Name__c = slrAccountMapUpdate.get(phoneStr).Id;
                 slrRecordMap.get(phoneStr).Status__c = 'Linked to Account';      
                 if(!NMRUtil.isEmpty(slrAccountMapUpdate.get(phoneStr).OOR_Secondary_Account_Owner__c)){
                     Task slrTask = new Task();
                     slrTask.WhatId = slrAccountMapUpdate.get(phoneStr).Id;
                     slrTask.Ownerid= slrAccountMapUpdate.get(phoneStr).OOR_Secondary_Account_Owner__c;
                     slrTask.Subject = slrRecordMap.get(phoneStr).Lead_Type__c + ' received for ' + slrRecordMap.get(phoneStr).Business_Name__c;
                     slrTask.priority= 'Normal';
                     slrTask.status = 'Not Started';
                     slrTask.RecordTypeId = nmrTaskRecordTypeID;
                     taskList.add(slrTask);
                 }                                   
             }
             //Else If Lead is present to match the SLR
             else if(slrLeadMap.containsKey(phoneStr) && !slrAccountMapUpdate.containsKey(phoneStr)){
                 Lead leadtoEnrich = new Lead();
                 leadtoEnrich = slrLeadMap.get(phoneStr);
                 slrRecordMap.get(phoneStr).Status__c = 'Linked to Lead';
                 slrRecordMap.get(phoneStr).Source_Lead_Id__c = leadtoEnrich.Id;
                 leadList.add(createUpdateLead(slrRecordMap.get(phoneStr), leadtoEnrich, headingMapSLR));
                 slrLeadMapUpdate.put(phonestr,createUpdateLead(slrRecordMap.get(phoneStr), leadtoEnrich, headingMapSLR));
             }
             //Else create a new Lead
             else {
                leadList.add(createUpdateLead(slrRecordMap.get(phoneStr),null, headingMapSLR));
                slrRecordMap.get(phoneStr).Status__c = 'New Lead';
                slrLeadMapUpdate.put(phoneStr,createUpdateLead(slrRecordMap.get(phoneStr),null, headingMapSLR));
  
             }
         }
         if(taskList.size()>0){
         try{
             insert taskList;   
         }
         catch(DMLException e){
             system.debug('Exception: ' +e);         
         }
         }
         if(slrLeadMapUpdate.size()>0){
             try{
                 upsert slrLeadMapUpdate.values();
                     //After upsert update the lead ids in the SLRs
                     for(Standardized_Lead_Request__c slr: slrRecordMap.Values()){
                             
                             slr.Related_Lead_Id__c = slrLeadMapUpdate.get(NMRUtil.formatPhone(slr.Business_Phone__c)).Id;
                             slr.Source_Lead_Id__c = slrLeadMapUpdate.get(NMRUtil.formatPhone(slr.Business_Phone__c)).Id;
                             slr.Office__c = slrLeadMapUpdate.get(NMRUtil.formatPhone(slr.Business_Phone__c)).NMR_Office__c;
                             slr.Channel__c = slrLeadMapUpdate.get(NMRUtil.formatPhone(slr.Business_Phone__c)).Channel__c;
                             slr.Assignment_Group__c = slrLeadMapUpdate.get(NMRUtil.formatPhone(slr.Business_Phone__c)).Assignment_Group__c;
                             
                     }
                 }
             catch(DMLException e){
                 system.debug('DML Exception'+e);
             }
         }
         
         //For Headings in Leads
         for(Cate__c singleCate:[Select Id,Standardized_Lead_Request__r.Source_Lead_Id__c,Standardized_Lead_Request__r.Related_Lead_Id__c,Standardized_Lead_Request__r.Business_Phone__c,Lead__r.phone,Description__c,Heading_Code__c,Restricted_Flag__c,Source__c,Weight__c,Source_Lead_I__c,YP_Cat__c from Cate__c where Lead__c IN:leadIdSet]){
             if(headingMapLead.containsKey(singleCate.Lead__r.phone)){
                 headingMapLead.get(singleCate.Lead__r.phone).add(singleCate);
             }
             else{
                
                 headingMapLead.put(singleCate.Lead__r.phone,new List<Cate__c>{singleCate});
             }
         }
         //Call create Heading method to create new headings under the lead      
         If(headingMap.size()>0){
             createLeadHeadings(headingMap,headingMapLead,slrLeadMapUpdate);
         }
     }
    
                 
     /**
     * Method to create unique headings for Lead from SLR
     */
     public static void createLeadHeadings(Map<Id,Cate__c> headingMap, Map<string,List<Cate__c>> headingMapLead,Map<string,Lead> slrLeadMapUpdate){
         List<Cate__c> headingInsertList = new List<Cate__c>();
         
         List<Cate__c> headings = headingMap.values();
         integer headingInt = headings.size();
         
         
        //Map<Id,Cate__c> headingMapNew = new Map<Id,Cate__c>();
        for(integer i=0;i<headingInt;i++){
            if(headingMapLead.containsKey(NMRUtil.formatPhone(headings[i].Standardized_Lead_Request__r.Business_Phone__c))){
            for(Cate__c leadsCate:headingMapLead.get(NMRUtil.formatPhone(headings[i].Standardized_Lead_Request__r.Business_Phone__c))){
                    if(headings[i].YP_Cat__c.equals(leadsCate.YP_Cat__c)){
                        headings.remove(i);
                        i--;
                        headingInt--;
                        break;                        
                    }
                 }
            }
        }   
        //Insert Headings
        for(Cate__c cateInsert:headings){
            if(slrLeadMapUpdate.containsKey(NMRUtil.formatPhone(cateInsert.Standardized_Lead_Request__r.Business_Phone__c)))
            {//create Headings for those which are not 'Linked to Account'
                //Continue to create headings
                Cate__c singleHeading = new Cate__c();
                singleHeading.Description__c = cateInsert.Description__c;
                singleHeading.Heading_Code__c = cateInsert.Heading_Code__c;
                singleHeading.Restricted_Flag__c = cateInsert.Restricted_Flag__c;
                singleHeading.Source__c = cateInsert.Source__c;
                singleHeading.Source_Lead_I__c = cateInsert.Source_Lead_I__c;
                singleHeading.Weight__c = cateInsert.Weight__c;
                singleHeading.YP_Cat__c = cateInsert.YP_Cat__c;
                singleHeading.Lead__c = slrLeadMapUpdate.get(NMRUtil.formatPhone(cateInsert.Standardized_Lead_Request__r.Business_Phone__c)).Id;
                    
                headingInsertList.add(singleHeading);
            }
        }  
        if(headingInsertList.size()>0){
            try{
                insert headingInsertList;
            }
            catch(DMLException ex){
                system.debug('DMLException '+ex);
            }
        }
     }
     
     /**
     * Method to create or update Lead from SLR (Update only if existing lead value is empty)
     */
     public static Lead createUpdateLead(Standardized_Lead_Request__c slr,Lead singleLead, Map<String, Cate__c> headingsMapSLR){
         
         if(singleLead==NULL){
             singleLead = new Lead();
         }

         if(singleLead.Id != null){
             //update lead type only if its not 'Inquiry'
             if((NMRUtil.isEmpty(singleLead.Lead_Type__c) && !NMRUtil.isEmpty(slr.Lead_Type__c))
                 || (!NMRUtil.isEmpty(slr.Lead_Type__c) && !(singleLead.Lead_Type__c).equalsIgnoreCase('Product Inquiry')))
                     singleLead.Lead_Type__c = slr.Lead_Type__c;
              
         }
                 singleLead.Last_Enrichment_Date__c = System.Now();
                 singleLead.Weight_Check_from_heading__c = slr.Weight_Check_from_heading__c;
                 //always override the No Of Employees
                 if(!NMRUtil.isEmpty(slr.No_Of_Employee__c))
                     singleLead.NumberOfEmployees = convertRangeToNumber(slr.No_Of_Employee__c);

                 //always override the Product Interest                     
                 if(!NMRUtil.isEmpty(slr.Offer_Product__c))  
                     singleLead.Offer_Product__c = slr.Offer_Product__c;

                 //always override the offer flow progress                     
                 if(!NMRUtil.isEmpty(slr.Offer_Flow_Progress__c))  
                     singleLead.Flow_Progress__c = slr.Offer_Flow_Progress__c;
                 
                 if(NMRUtil.isEmpty(singleLead.Lead_Type__c) && !NMRUtil.isEmpty(slr.Lead_Type__c))
                     singleLead.Lead_Type__c = slr.Lead_Type__c;
                 
                 if(NMRUtil.isEmpty(singleLead.LeadSource) && !NMRUtil.isEmpty(slr.Lead_Source__c))
                     singleLead.LeadSource = slr.Lead_Source__c;
                 
                 if(NMRUtil.isEmpty(singleLead.phone) && !NMRUtil.isEmpty(slr.Business_Phone__c)){
                     singleLead.phone = NMRUtil.formatPhone(slr.Business_Phone__c);
                     singleLead.DoNotCall = slr.On_Do_Not_Call_List__c;
                 }
                 
                 if(NMRUtil.isEmpty(singleLead.Sales_Channel__c) && !NMRUtil.isEmpty(slr.Sales_Channel__c))
                     singleLead.Sales_Channel__c = slr.Sales_Channel__c;
                 
                 if(NMRUtil.isEmpty(singleLead.FirstName) && !NMRUtil.isEmpty(slr.Contact_First_Name__c))
                     singleLead.FirstName = slr.Contact_First_Name__c;
                 
                 if(NMRUtil.isEmpty(singleLead.LastName) && !NMRUtil.isEmpty(slr.Contact_Last_Name__c))
                     singleLead.LastName = slr.Contact_Last_Name__c;
                 
                 if(NMRUtil.isEmpty(singleLead.Email) && !NMRUtil.isEmpty(slr.Contact_Email__c))
                     singleLead.Email = slr.Contact_Email__c;
                 
                 if(NMRUtil.isEmpty(singleLead.LMCListing_Id__c) && !NMRUtil.isEmpty(slr.LMC_Listing_Id__c))
                     singleLead.LMCListing_Id__c = slr.LMC_Listing_Id__c;
                        
                 if(NMRUtil.isEmpty(singleLead.Company) && !NMRUtil.isEmpty(slr.Business_Name__c))
                     singleLead.Company = slr.Business_Name__c;
                 
                 if(NMRUtil.isEmpty(singleLead.Website) && !NMRUtil.isEmpty(slr.Business_Website_URL__c))
                     singleLead.Website = slr.Business_Website_URL__c;
                  
                 if(NMRUtil.isEmpty(singleLead.Street) && !NMRUtil.isEmpty(slr.Address_Line__c))
                     singleLead.Street = slr.Address_Line__c;
                 
                 if(NMRUtil.isEmpty(singleLead.City) && !NMRUtil.isEmpty(slr.City__c))
                     singleLead.City = slr.City__c;
                 
                 if(NMRUtil.isEmpty(singleLead.State) && !NMRUtil.isEmpty(slr.State__c))
                     singleLead.State = slr.State__c;
                 
                 if(NMRUtil.isEmpty(singleLead.PostalCode) && !NMRUtil.isEmpty(slr.Zip__c))
                     singleLead.PostalCode = slr.Zip__c;
                 
                 if(NMRUtil.isEmpty(singleLead.Business_Description__c) && !NMRUtil.isEmpty(slr.Business_Description__c))
                     singleLead.Business_Description__c = slr.Business_Description__c;
                 
                 if(NMRUtil.isEmpty(singleLead.In_Business_Since__c) && !NMRUtil.isEmpty(slr.In_Business_Since__c))
                     singleLead.In_Business_Since__c = slr.In_Business_Since__c;
                            
                 if(NMRUtil.isEmpty(singleLead.Advertiser_Comments__c) && !NMRUtil.isEmpty(slr.Advertiser_Comments__c))
                     singleLead.Advertiser_Comments__c = slr.Advertiser_Comments__c;
                 
                 if(NMRUtil.isEmpty(singleLead.Campaign_Medium__c) && !NMRUtil.isEmpty(slr.Campaign_Medium__c))
                     singleLead.Campaign_Medium__c = slr.Campaign_Medium__c;
                 
                 if(NMRUtil.isEmpty(singleLead.Campaign_Source__c) && !NMRUtil.isEmpty(slr.Campaign_Source__c))
                     singleLead.Campaign_Source__c = slr.Campaign_Source__c;
                 
                 if(NMRUtil.isEmpty(singleLead.Campaign_Name__c) && !NMRUtil.isEmpty(slr.Campaign_Name__c))
                     singleLead.Campaign_Name__c = slr.Campaign_Name__c;
                 
                 if(NMRUtil.isEmpty(singleLead.Campaign_Content__c) && !NMRUtil.isEmpty(slr.Campaign_Content__c))
                     singleLead.Campaign_Content__c = slr.Campaign_Content__c;
                 
                 if(NMRUtil.isEmpty(singleLead.Campaign_Term__c) && !NMRUtil.isEmpty(slr.Campaign_Term__c))
                     singleLead.Campaign_Term__c = slr.Campaign_Term__c;
                 
                 if(NMRUtil.isEmpty(singleLead.Campaign_Category__c) && !NMRUtil.isEmpty(slr.Campaign_Category__c))
                     singleLead.Campaign_Category__c = slr.Campaign_Category__c;
                 
                 if(NMRUtil.isEmpty(singleLead.Chain_ID__c) && !NMRUtil.isEmpty(slr.Chain_ID__c))
                     singleLead.Chain_ID__c = slr.Chain_ID__c;
                     
                 if(NMRUtil.isEmpty(singleLead.Customer_Id__c) && !NMRUtil.isEmpty(slr.Customer_Id__c))
                     singleLead.Customer_Id__c = slr.Customer_Id__c;
                     
                 if(NMRUtil.isEmpty(singleLead.AKAs__c) && !NMRUtil.isEmpty(slr.AKAs__c))
                     singleLead.AKAs__c = slr.AKAs__c;
                     
                 if(singleLead.ASCP_Suscriber_Created_Date__c == null && slr.ASCP_Account_Created_Date__c != null)
                     singleLead.ASCP_Suscriber_Created_Date__c = slr.ASCP_Account_Created_Date__c;
                 
                 if(NMRUtil.isEmpty(singleLead.Offer_Type__c) && !NMRUtil.isEmpty(slr.Offer_Type__c))
                     singleLead.Offer_Type__c = slr.Offer_Type__c;
                     
                 if(NMRUtil.isEmpty(singleLead.Ad_Size__c) && !NMRUtil.isEmpty(slr.Ad_Size__c))
                     singleLead.Ad_Size__c = slr.Ad_Size__c;
                     
                 if(NMRUtil.isEmpty(singleLead.Ad_Spend__c) && !NMRUtil.isEmpty(slr.Ad_Spending__c))
                     singleLead.Ad_Spend__c = slr.Ad_Spending__c;
                     
                 if(NMRUtil.isEmpty(singleLead.Service_Products__c) && !NMRUtil.isEmpty(slr.Service_Products__c))
                     singleLead.Service_Products__c = slr.Service_Products__c;    
                  
                 if(NMRUtil.isEmpty(singleLead.YP_Id__c) && !NMRUtil.isEmpty(slr.YP_Id__c))
                     singleLead.YP_Id__c = slr.YP_Id__c;
                 
                 if(NMRUtil.isEmpty(singleLead.Friendly_Name__c) && !NMRUtil.isEmpty(slr.Lead_Source_Friendly_Name__c))
                     singleLead.Friendly_Name__c = slr.Lead_Source_Friendly_Name__c;
                     
                 if(NMRUtil.isEmpty(singleLead.ECommerce_CID__c) && !NMRUtil.isEmpty(slr.ECommerce_CID__c))
                     singleLead.ECommerce_CID__c = slr.ECommerce_CID__c;
                 
                 //Checkbox. Hence null validation not required.    
                 singleLead.Send_Special_Offers__c = slr.Send_Special_Offers__c;
                     
                 if(NMRUtil.isEmpty(singleLead.Sales_Lead_Id__c) && !NMRUtil.isEmpty(slr.Source_Lead_Id__c))
                     singleLead.Sales_Lead_Id__c = slr.Source_Lead_Id__c;
                 
                 if(NMRUtil.isEmpty(singleLead.YP_Cat_Id__c) && !NMRUtil.isEmpty(slr.YP_Cat_Id__c))
                     singleLead.YP_Cat_Id__c = slr.YP_Cat_Id__c;
                         
                 if(NMRUtil.isEmpty(singleLead.Primary_Heading__c) && !headingsMapSLR.isEmpty() &&
                     headingsMapSLR.containsKey(NMRUtil.formatPhone(slr.Business_Phone__c)))
                 {
                     String description = (headingsMapSLR.get(NMRUtil.formatPhone(slr.Business_Phone__c))).Description__c;
                     if(!NMRUtil.isEmpty(description) && description.length() > 50)
                         description = description.substring(0,50);
                     singleLead.Primary_Heading__c = description;
                 }   
                 
                 //DNC Phones and flags 
                 
                 if(NMRUtil.isEmpty(singleLead.NMR_Additional_Phone_1__c) && !NMRUtil.isEmpty(slr.Additional_Phone_Number_3__c)){
                     singleLead.NMR_Additional_Phone_1__c = NMRUtil.formatPhone(slr.Additional_Phone_Number_3__c);
                     singleLead.Do_Not_Call_1__c = slr.Do_Not_Call_3__c;
                 }
                 
                 if(NMRUtil.isEmpty(singleLead.NMR_Additional_Phone_2__c) && !NMRUtil.isEmpty(slr.Additional_Phone_Number_1__c)){
                     singleLead.NMR_Additional_Phone_2__c = NMRUtil.formatPhone(slr.Additional_Phone_Number_1__c);
                     singleLead.Do_Not_Call_2__c = slr.Do_Not_Call_1__c;
                 }
                 
                 if(NMRUtil.isEmpty(singleLead.NMR_Additional_Phone_3__c) && !NMRUtil.isEmpty(slr.Additional_Phone_Number_3__c)){
                     singleLead.NMR_Additional_Phone_3__c = NMRUtil.formatPhone(slr.Additional_Phone_Number_3__c);
                     singleLead.Do_Not_Call_3__c = slr.Do_Not_Call_3__c;
                 }
                 
                 if(NMRUtil.isEmpty(singleLead.NMR_Additional_Phone_4__c) && !NMRUtil.isEmpty(slr.Additional_Phone_Number_2__c)){
                     singleLead.NMR_Additional_Phone_4__c = NMRUtil.formatPhone(slr.Additional_Phone_Number_2__c);
                     singleLead.Do_Not_Call_4__c = slr.Do_Not_Call_2__c;
                 }

                 if(NMRUtil.isEmpty(singleLead.Customer_Id__c))
                     singleLead.recordtypeid = nmrLeadRecordTypeID;
                 else
                     singleLead.recordtypeid = nmrLeadCustIdRecordTypeID;
                     
                     
                 //setting Lead Status based on lead type/source and DNC
                 
                 if(singleLead.DoNotCall && ('Listing'.equalsIgnoreCase(singleLead.Lead_Type__c) 
                         || 'IFIKI'.equalsIgnoreCase(singleLead.LeadSource)))
                 {
                     singleLead.status = 'Invalid';
                     singleLead.Bad_Reason__c = 'Do Not Call';
                 } else if (singleLead.DoNotCall && 'Inquiry'.equalsIgnoreCase(singleLead.Lead_Type__c))
                 {
                     singleLead.status = 'New';
                     singleLead.Bad_Reason__c = 'Do Not Call';
                 } else 
                 {
                     singleLead.Status = 'New';
                 }
                     
                 return singleLead;
     }
     
     /**
     * Method to convert a number range (a - b) to the higher number. (ie. b)
     */
     private static integer convertRangeToNumber(String noOfEmp){
         
         integer indexMinus = noOfEmp.indexOf('-');
         integer indexPlus = noOfEmp.indexOf('+');
         
         if(indexMinus != -1)
             noOfEmp = noOfEmp.subString(indexMinus + 1);
         else if (indexPlus != -1)
             noOfEmp = noOfEmp.subString(0, indexPlus);
          
         return Integer.valueOf(noOfEmp.trim());
     
     }
 }